package io.renren.modules.communityService.service.impl;

import cn.hutool.core.util.ObjectUtil;
import io.renren.modules.communityService.dto.MiniAppDbUserInfoDTO;
import io.renren.modules.communityService.dto.MiniAppDbUserLoginDTO;
import io.renren.modules.communityService.dto.MiniAppDbUserPayDTO;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.communityService.dao.DbUserDao;
import io.renren.modules.communityService.entity.DbUserEntity;
import io.renren.modules.communityService.dto.DbUserDTO;
import io.renren.modules.communityService.vo.DbUserVO;
import io.renren.modules.communityService.service.DbUserService;
import io.renren.modules.communityService.conver.DbUserConver;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;


@Service("dbUserService")
public class DbUserServiceImpl extends ServiceImpl<DbUserDao, DbUserEntity> implements DbUserService {

    @Override
    public PageUtils<DbUserVO> queryPage(DbUserDTO dbUser) {
        IPage<DbUserEntity> page = baseMapper.selectPage(
                new Query<DbUserEntity>(dbUser).getPage(),
                new QueryWrapper<DbUserEntity>()
        );

        return PageUtils.<DbUserVO>page(page).setList(DbUserConver.MAPPER.conver(page.getRecords()));
    }
    @Override
    public DbUserVO getById(Integer id) {
        return DbUserConver.MAPPER.conver(baseMapper.selectById(id));
    }

    @Override
    public boolean save(DbUserDTO dbUser) {
        DbUserEntity dbUserEntity = DbUserConver.MAPPER.converDTO(dbUser);
        return this.save(dbUserEntity);
    }

    @Override
    public boolean updateById(DbUserDTO dbUser) {
        DbUserEntity dbUserEntity = DbUserConver.MAPPER.converDTO(dbUser);
        return this.updateById(dbUserEntity);
    }

    @Override
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    @Override
    public boolean removeByIds(Collection<? extends Serializable> ids) {
        return super.removeByIds(ids);
    }


    @Override
    public DbUserEntity login(MiniAppDbUserLoginDTO dbUser) {
        DbUserEntity one = this.getOne(new QueryWrapper<DbUserEntity>().lambda()
                .eq(DbUserEntity::getOpenid,dbUser.getOpenid())
                .last("limit 1")
        );
        if (ObjectUtil.isNull(one)) {
            one = new DbUserEntity();
            one.setOpenid(dbUser.getOpenid());
            one.setUserNickname(dbUser.getUserNickname());
            this.save(one);
        }
        return one;
    }

    @Override
    public DbUserVO info(MiniAppDbUserInfoDTO dbUser) {
        DbUserVO dbUserVO = this.getById(dbUser.getId());
        return dbUserVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void pay(MiniAppDbUserPayDTO dbUser) {
        DbUserEntity one = new DbUserEntity();
        one.setId(dbUser.getId());
        one.setElectricityFee(dbUser.getElectricityFee());
        one.setWaterFee(dbUser.getWaterFee());
        this.updateById(one);
    }

}
